package com.study.controller;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.study.model.pojo.Inventory;
import com.study.model.pojo.Kind;
import com.study.model.pojo.Loss;
import com.study.model.pojo.Product;
import com.study.model.pojo.Warehouse;
import com.study.services.InventoryService;
import com.study.services.KindService;
import com.study.services.LossService;
import com.study.services.ProductService;
import com.study.services.WarehouseService;

@RestController
@CrossOrigin
public class InventoryController {
	@Autowired
	InventoryService iService;
	@Autowired
	LossService lService;
	@Autowired
	WarehouseService wService;
	@Autowired
	ProductService pService;
	@Autowired
	KindService kService;

	/*
	 * @RequestMapping("/findInventoryAndProductByinvId") public ModelAndView
	 * findInventoryAndProductByinvId() { List<Inventory>
	 * list=iService.findInventoryAndProduct(); for (Inventory inventory : list) {
	 * System.out.println("xinxi: "+inventory.getInvId()+"---"+inventory.getProduct(
	 * ).getKinName()); } ModelAndView mv=new ModelAndView(); mv.addObject("list",
	 * list); return mv; }
	 */

	/**
	 * 查询库存信息及报损信息
	 * 
	 * @return
	 */
	@RequestMapping("/findInventoryAndLoss")
	public JSONObject findInventoryAndLoss(String warName, String kinName) {
		JSONObject json = new JSONObject();
		JSONArray json1 = new JSONArray();
		JSONArray json2 = new JSONArray();
		JSONArray json3 = new JSONArray();
		JSONArray json4 = new JSONArray();
		JSONArray json5 = new JSONArray();
		JSONArray json6 = new JSONArray();
		// 查询所有的库存信息
		if ((warName == null || warName.length() == 0 || warName.equals("查询全部"))
				&& (kinName == null || kinName.length() == 0 || kinName.equals("查询全部"))) {
			List<Inventory> list = iService.findInventoryAndProduct();
			for (Inventory i : list) {
				JSONObject jo = new JSONObject();
				System.out.println(i.getProduct().getProName());
				jo.put("invId", i.getInvId());
				jo.put("proId", i.getProduct().getProName());
				jo.put("warName", i.getWarName());
				jo.put("invUnit", i.getInvUnit());
				jo.put("invNum", i.getInvNum());
				jo.put("invLow", i.getInvLow());
				jo.put("invHeight", i.getInvHeight());
				jo.put("invStatic", i.getInvStatic());
				jo.put("invNote", i.getInvNote());
				jo.put("proSpe", i.getProduct().getProSpe());
				jo.put("proPic", i.getProduct().getProPic());
				jo.put("proUnit", i.getProduct().getProUnit());
				jo.put("kinName", i.getProduct().getKinName());
				json1.add(jo);
			}
		}else if (warName.equals("查询全部")) {
			HashMap map = new HashMap<>();
			map.put("warName", null);
			map.put("kinName", kinName);
			List<Inventory> list = iService.findByWarName(map);
			for (Inventory i : list) {
				JSONObject jo = new JSONObject();
				System.out.println(i.getProduct().getProName());
				jo.put("invId", i.getInvId());
				jo.put("proId", i.getProduct().getProName());
				jo.put("warName", i.getWarName());
				jo.put("invUnit", i.getInvUnit());
				jo.put("invNum", i.getInvNum());
				jo.put("invLow", i.getInvLow());
				jo.put("invHeight", i.getInvHeight());
				jo.put("invStatic", i.getInvStatic());
				jo.put("invNote", i.getInvNote());
				jo.put("proSpe", i.getProduct().getProSpe());
				jo.put("proPic", i.getProduct().getProPic());
				jo.put("proUnit", i.getProduct().getProUnit());
				jo.put("kinName", i.getProduct().getKinName());
				json1.add(jo);
			}
		}else if (kinName.equals("查询全部")) {
			HashMap map = new HashMap<>();
			map.put("warName", warName);
			map.put("kinName", null);
			List<Inventory> list = iService.findByWarName(map);
			for (Inventory i : list) {
				JSONObject jo = new JSONObject();
				System.out.println(i.getProduct().getProName());
				jo.put("invId", i.getInvId());
				jo.put("proId", i.getProduct().getProName());
				jo.put("warName", i.getWarName());
				jo.put("invUnit", i.getInvUnit());
				jo.put("invNum", i.getInvNum());
				jo.put("invLow", i.getInvLow());
				jo.put("invHeight", i.getInvHeight());
				jo.put("invStatic", i.getInvStatic());
				jo.put("invNote", i.getInvNote());
				jo.put("proSpe", i.getProduct().getProSpe());
				jo.put("proPic", i.getProduct().getProPic());
				jo.put("proUnit", i.getProduct().getProUnit());
				jo.put("kinName", i.getProduct().getKinName());
				json1.add(jo);
			}
		}else {
			HashMap map = new HashMap<>();
			map.put("warName", warName);
			map.put("kinName", kinName);
			List<Inventory> list = iService.findByWarName(map);
			for (Inventory i : list) {
				JSONObject jo = new JSONObject();
				System.out.println(i.getProduct().getProName());
				jo.put("invId", i.getInvId());
				jo.put("proId", i.getProduct().getProName());
				jo.put("warName", i.getWarName());
				jo.put("invUnit", i.getInvUnit());
				jo.put("invNum", i.getInvNum());
				jo.put("invLow", i.getInvLow());
				jo.put("invHeight", i.getInvHeight());
				jo.put("invStatic", i.getInvStatic());
				jo.put("invNote", i.getInvNote());
				jo.put("proSpe", i.getProduct().getProSpe());
				jo.put("proPic", i.getProduct().getProPic());
				jo.put("proUnit", i.getProduct().getProUnit());
				jo.put("kinName", i.getProduct().getKinName());
				json1.add(jo);
			}
		}
		// 查询所有报损信息
		List<Loss> list2 = lService.findAll();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		for (Loss l : list2) {
			JSONObject jo = new JSONObject();
			System.out.println(l.getProduct().getProId());
			jo.put("losId", l.getLosId());
			jo.put("proId", l.getProduct().getProId());
			jo.put("proName", l.getProduct().getProName());
			jo.put("losDate", sdf.format(l.getLosDate()));
			jo.put("losWhy", l.getLosWhy());
			jo.put("losWay", l.getLosWay());
			jo.put("warName", l.getWarName());
			jo.put("losNum", l.getLosNum());
			jo.put("losNote", l.getLosNote());
			jo.put("losStatic", l.getLosStatic());
			json2.add(jo);
		}
		// 给一个"全部查询"字段,带有全部查询
		JSONObject jo1 = new JSONObject();
		jo1.put("warId", "war0000");
		jo1.put("warName", "查询全部");
		json3.add(jo1);
		// 查询所有仓库信息
		List<Warehouse> list3 = wService.findAll();
		for (Warehouse w : list3) {
			JSONObject jo = new JSONObject();
			jo.put("warId", w.getWarId());
			jo.put("warName", w.getWarName());
			System.out.println(jo);
			json3.add(jo);
		}
		// 给一个"全部查询"字段,不带有全部查询
		List<Warehouse> list4 = wService.findAll();
		for (Warehouse w : list4) {
			JSONObject jo = new JSONObject();
			jo.put("warId", w.getWarId());
			jo.put("warName", w.getWarName());
			System.out.println(jo);
			json4.add(jo);
		}
		// 查询全部产品信息
		List<Product> list5 = pService.findAll();
		for (Product p : list5) {
			JSONObject jo = new JSONObject();
			jo.put("proId", p.getProId());
			jo.put("proName", p.getProName());
			json5.add(jo);
		}
		//查询全部产品分类
		List<Kind> list6 = kService.findAll();
		for (Kind k : list6) {
			JSONObject jo = new JSONObject();
			jo.put("kinId", k.getKinId());
			jo.put("kinName", k.getKinName());
			json6.add(jo);
		}
		json.put("inventory", json1);
		json.put("loss", json2);
		json.put("warNames", json3);
		json.put("warNames2", json4);
		json.put("product", json5);
		json.put("kind", json6);
		return json;
	}

	/**
	 * 新增报损
	 * 
	 * @param loss  报损信息
	 * @param proId 产品名称
	 * @return
	 */
	@RequestMapping("/addloss")
	public JSONObject addloss(Loss loss, String proId) {
		JSONObject json = new JSONObject();
		loss.setLosId("los003");
		// 通过前台参数查询产品id
		String proid = pService.findByproName(proId);
		// 装入map
		HashMap map = new HashMap<>();
		map.put("losId", loss.getLosId());
		map.put("proId", proid);
		map.put("losWhy", loss.getLosWhy());
		map.put("losWay", loss.getLosWay());
		map.put("warName", loss.getWarName());
		map.put("losNum", loss.getLosNum());
		map.put("losNote", loss.getLosNote());
		map.put("losStatic", "待审核");
		String returnString = lService.addloss(map, proid, loss);
		// 判断是否添加成功
		json.put("ans", returnString);
		return json;
	}

	/**
	 * 预警设置
	 * 
	 * @param inventory
	 * @return
	 */
	@RequestMapping("/editInventory")
	public JSONObject editInventory(Inventory inventory) {
		JSONObject json = new JSONObject();
		// 装入map
		System.out.println("inventory=" + inventory.getInvLow());
		HashMap map = new HashMap<>();
		map.put("invId", inventory.getInvId());
		map.put("invLow", inventory.getInvLow());
		map.put("invHeight", inventory.getInvHeight());
		map.put("invNote", inventory.getInvNote());
		iService.editInventory(map);// 修改信息
		json.put("ans", "n");
		return json;
	}

	/**
	 * 通过审核
	 * 
	 * @param losId 报损编号
	 * @return
	 */
	@RequestMapping("/losspass")
	public JSONObject losspass(String losId) {
		JSONObject json = new JSONObject();
		lService.losspass(losId);
		json.put("ans", "y");
		return json;
	}

	/**
	 * 删除报损信息
	 * 
	 * @param losId
	 * @return
	 */
	@RequestMapping("/delloss")
	public JSONObject delloss(String losId) {
		JSONObject json = new JSONObject();
		lService.delLoss(losId);
		json.put("ans", "y");
		return json;
	}
}
